import { i18n } from './i18n'

const getDefaultKeyBoard = () => {
    return {
        left: 'ArrowLeft',
        right: 'ArrowRight',
        hardDown: 'Space',
        softDown: 'KeyC',
        hold: 'ShiftLeft',
        // 顺时针旋转
        rotate: 'ArrowDown',
        // 逆时针旋转
        rotateBack: 'KeyV',
        pause: 'Escape',
        restart: 'KeyR'
    }
}

const showKeyBoardText = (key = 'left') => {
    const t = i18n.global.t // 获取翻译函数
    switch (key) {
        case 'left':
            return t('keyboard.moveLeft');
        case 'right':
            return t('keyboard.moveRight');
        case 'hardDown':
            return t('keyboard.hardDrop');
        case 'softDown':
            return t('keyboard.softDrop');
        case 'hold':
            return t('keyboard.hold');
        case 'rotate':
            return t('keyboard.rotate');
        case 'rotateBack':
            return t('keyboard.rotateBack');
        case 'pause':
            return t('keyboard.pause');
        case 'restart':
            return t('keyboard.restart');
        default:
            return key;
    }
}

const getDefaultUserConfig = () => {
    return {
        lang: 'en',
        autoPause: true, // 窗口失去焦点 自动暂停
    }
}

const getDefaultTetrisConfig = () => {
    return {
        das: 100,
        arr: 20,
        sortArr: 20,
    }
}

const showTetrisConfigText = (key = 'das') => {
    const t = i18n.global.t
    switch (key) {
        case 'lang':
            return t('config.language');
        case 'autoPause':
            return t('config.autoPause');
        default:
            return key;
    }
}

const showTetrisHandlingText = (key = 'das') => {
    const t = i18n.global.t
    switch (key) {
        case 'das':
            return t('config.das');
        case 'arr':
            return t('config.arr');
        case 'sortArr':
            return t('config.sortArr');
        default:
            return key;
    }
}

const getTetrisConfigRange = (key = 'das') => {
    switch (key) {
        case 'das':
            return [0, 250];
        case 'arr':
            return [0, 100];
        case 'sortArr':
            return [0, 100];
        default:
            return [0, 1];
    }
}

// 虚拟按键
const virtualButtonInfo = () => {
    return [
        {
            action: 'left',
            text: '左移',
            position: { // 中心坐标 单位: 百分比
                x: 10,
                y: 50
            },
            size: 20, // 按钮大小 单位 vmin
        },
        {
            action: 'right',
            text: '右移',
            position: {
                x: 90,
                y: 50
            },
            size: 20,
        },
        {
            action: 'hardDown',
            text: '硬降',
            position: {
                x: 90,
                y: 80
            },
            size: 20,
        },
        {
            action: 'softDown',
            text: '软降',
            position: {
                x: 10,
                y: 80
            },
            size: 20,
        },
        {
            action: 'hold',
            text: '暂存',
            position: {
                x: 50,
                y: 80
            },
            size: 20,
        },
        {
            action: 'rotate',
            text: '顺时针旋转',
            position: {
                x: 30,
                y: 80
            },
            size: 20,
        },
        {
            action: 'rotateBack',
            text: '逆时针旋转',
            position: {
                x: 70,
                y: 80
            },
            size: 20,
        },
    ]
}

// 声音设置
const getDefaultSoundConfig = () => {
    return {
        BGM: true, // bgm 开关
        SOUND: true, // 音效开关
        'BGM VOLUME': 50, // bgm音量
        'SOUND VOLUME': 50, // 音效音量
    }
}

const getKeyBoard = () => {
    const keyBoard = localStorage.getItem('keyBoard')
    return keyBoard ? JSON.parse(keyBoard) : getDefaultKeyBoard()
}

const setKeyBoard = (data: any) => {
    let keyBoard = localStorage.getItem('keyBoard') as any
    keyBoard = keyBoard ? JSON.parse(keyBoard) : getDefaultKeyBoard()
    Object.keys(data).forEach(key => {
        if (data[key] && keyBoard[key]) {
            keyBoard[key] = data[key]
        }
    })
    localStorage.setItem('keyBoard', JSON.stringify(keyBoard))
}

const getTetrisConfig = () => {
    const tetrisConfig = localStorage.getItem('tetrisConfig')
    return tetrisConfig ? JSON.parse(tetrisConfig) : getDefaultTetrisConfig()
}

const setTetrisConfig = (data: any) => {
    let tetrisConfig = localStorage.getItem('tetrisConfig') as any
    tetrisConfig = tetrisConfig ? JSON.parse(tetrisConfig) : getDefaultTetrisConfig()
    Object.keys(data).forEach(key => {
        tetrisConfig[key] = data[key]
    })
    localStorage.setItem('tetrisConfig', JSON.stringify(tetrisConfig))
}

const getUserConfig = () => {
    const userConfig = localStorage.getItem('userConfig')
    return userConfig ? JSON.parse(userConfig) : getDefaultUserConfig()
}

const setUserConfig = (data: any) => {
    let userConfig = localStorage.getItem('userConfig') as any
    userConfig = userConfig ? JSON.parse(userConfig) : getDefaultUserConfig()
    Object.keys(data).forEach(key => {
        userConfig[key] = data[key]
    })
    localStorage.setItem('userConfig', JSON.stringify(userConfig))
}

const getSoundConfig = () => {
    const soundConfig = localStorage.getItem('soundConfig')
    return soundConfig ? JSON.parse(soundConfig) : getDefaultSoundConfig()
}

const setSoundConfig = (data: any) => {
    let soundConfig = localStorage.getItem('soundConfig') as any
    soundConfig = soundConfig ? JSON.parse(soundConfig) : getDefaultSoundConfig()
    Object.keys(data).forEach(key => {
        soundConfig[key] = data[key]
    })
    localStorage.setItem('soundConfig', JSON.stringify(soundConfig))
}

const getVirtualButtonConfig = () => {
    const virtualButtonConfig = localStorage.getItem('virtualButtonConfig')
    return virtualButtonConfig ? JSON.parse(virtualButtonConfig) : virtualButtonInfo()
    // return virtualButtonInfo()
}

const setVirtualButtonConfig = (data: any) => {
    data = data.map((item: any) => {
        if (!item.size) item.size = 20
        return item
    })
    localStorage.setItem('virtualButtonConfig', JSON.stringify(data))
}

export {
    getKeyBoard,
    setKeyBoard,
    getTetrisConfig,
    setTetrisConfig,
    virtualButtonInfo,
    getVirtualButtonConfig,
    setVirtualButtonConfig,
    showKeyBoardText,
    showTetrisConfigText,
    showTetrisHandlingText,
    getTetrisConfigRange,
    getSoundConfig,
    setSoundConfig,
    getUserConfig,
    setUserConfig,
}
